import { Component, OnInit, ViewChild } from '@angular/core';

import { RemoveTableService } from 'app/public/serve/remove-table/remove-table.service';
import { InitService } from 'app/public/serve/init/init.service';
import { HintsService } from 'app/public/serve/hints/hints.service';
import { SysconfigTableComponent } from './sysconfig-table/sysconfig-table.component';
import { Service } from '../../server/server';

import { sysconfig } from './sysconfig.data';
import { sysconfigUrl } from './sysconfig.url';

declare var $:any;

@Component({
  selector: 'app-system-sysconfig',
  templateUrl: './sysconfig.component.html',
  styleUrls: ['./sysconfig.component.css']
})
export class SystemSysconfigComponent implements OnInit {

  @ViewChild("table") table : SysconfigTableComponent;

  tableNumber : number = 0;

  right: any = { "system" : {}, "sysconfig" : {} };
  constructor(
    public initService: InitService,
    public removeTableService: RemoveTableService,
    public server: Service,
    private hintsService: HintsService
  ) {
    this.right = $.extend(this.right, server.getRight());
  }
  
  isActive= this.server.isActive;
  ngOnInit() {
    this.server.eventEmit.subscribe((value: any) => { this.isActive=value; });
  }

  ngDoCheck(){
    this.right = $.extend(this.right, this.server.getRight());
  }

  /**
   * 批量启用
   */ 
  _enable() {
    this._status([1]);
  }

  /**
   * 批量禁用
   */ 
  _disable() {
    this._status([0]);
  }

  /**
   * 分页
   * @param pagination 
   */ 
  _pagination(pagination: Array<any>) {
    sysconfig.pagination.pageNumber = pagination[0].pageNumber;
    sysconfig.pagination.pageSize   = pagination[0].pageSize;
    sysconfig.pagination.naturalNum = (sysconfig.pagination.pageNumber - 1) * sysconfig.pagination.pageSize;
    this.table._table();
  }
  
  /**
   * 检索
   * @param val 
   */ 
  _retrieval(val: string) {
    sysconfig.retrieval.queryCriteria = val.trim();
    this.table._table();
  }

  /**
   * 批量启用、禁用
   * @param val 
   */ 
  private _status(val: any) {
    this.removeTableService
        .confirmRemove('sysConfigId', this.server.baseUrl +sysconfigUrl.modifyStatus, 'enabled', val)
        .then(res=> {
          if(res.result_code='0000') {
            this.table._table();
          }
          this.hintsService.messages(res.message, res.result_code);
          return
        }).catch(err=> {
          this.hintsService.messages('error', 'error');
          return
        })
  }

}